﻿using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer;
using EPiServer.Core;
using EPiServer.Web.Hosting;

namespace OpenWaves.EPiServer.ImageBrowser
{
    public class AvailableVpp
    {
        public IEnumerable<VppDto> GetVppListing()
        {
            var vppList = new List<VppDto>();

            var supportedVirtualPathProvider =
                DataFactory.Instance.GetProvider(ContentReference.StartPage).SupportedVirtualPathProvider;
            if (supportedVirtualPathProvider == null)
            {
                return Enumerable.Empty<VppDto>();
            }
            foreach (var virtualPathProvider in supportedVirtualPathProvider)
            {
                var virtualPathVersioningProvider = virtualPathProvider as VirtualPathVersioningProvider;
                if (virtualPathVersioningProvider != null)
                {
                    var displayName = virtualPathVersioningProvider.ConfigurationParameters["virtualName"];
                    if (string.IsNullOrEmpty(displayName))
                    {
                        displayName = virtualPathVersioningProvider.ProviderName;
                    }
                    vppList.Add(new VppDto
                        {
                            Path = virtualPathVersioningProvider.VirtualPathRoot,
                            DisplayName = displayName,
                            Name = virtualPathVersioningProvider.ProviderName
                        });
                }
            }
            return vppList;
        }
    }

    [Serializable]
    public class VppDto
    {
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public string Path { get; set; }
    }
}
